//
//  DetailOfPhotographViewController.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/14.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "DetailOfPhotographViewController.h"
#import "DetailOfPhotographTableViewCell.h"
#import "LBAFNetworking.h"
#import "LuBDefine.h"
#import "DetailOfPhotograph.h"
#import "StoryViewController.h"
#import "DetailOfNumViewController.h"
#import "FnumViewController.h"
#import <MJRefresh.h>
#import "CircleOfPetViewController.h"

@interface DetailOfPhotographViewController ()<UITableViewDelegate, UITableViewDataSource, DetailOfPhotographTableViewCellDelegate>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)DetailOfPhotograph *detailOfPhotograph;

@end

@implementation DetailOfPhotographViewController

- (void)dealloc{
    [_uid release];
    [_petname release];
    [_tableView release];
    [_detailOfPhotograph release];
    [super dealloc];
}
- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsDefaultPrompt];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];

    // Do any additional setup after loading the view.
}

- (void)setUid:(NSString *)uid
{
    if (_uid != uid) {
        [_uid release];
        _uid = [uid copy];
    }
    [self downloadData];
}

- (void)downloadData{
    NSString *url =[NSString stringWithFormat:@"http://api.epetbar.com/gutouv2/user.html?uid=%@&getpets=1&do=info",_uid];

    [LBNetWork handleGETWithUrlString:url parameters:nil showHuD:YES onView:self.view successfulBlock:^(id responseObject) {
        
        NSDictionary *detailDictionary = responseObject;
        if (![[[detailDictionary objectForKey:@"data"] objectForKey:@"pets"] count]) {
            UIView *error = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, CHEIGHT)];
            error.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:error];

            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, CHEIGHT)];
            label.backgroundColor = [UIColor groupTableViewBackgroundColor];
            label.textColor = [UIColor grayColor];
            label.text = @"此用户暂不开放";
            label.textAlignment = NSTextAlignmentCenter;
            [error addSubview:label];
            [label release];
        }else{
         self.detailOfPhotograph = [DetailOfPhotograph detailOfPhotographWithDictionary:detailDictionary];
        [self.tableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (void)errorAction:(UIButton *)button{
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createDetialOfPhotographViewController];
    }
    return self;
}

- (void)createDetialOfPhotographViewController{
    self.view.backgroundColor = [UIColor grayColor];
    UIImageView *bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"LuBBG.jpg"]];
    [self.view addSubview:bgImageView];
    [bgImageView release];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(0, 0, 44, 44);
//    [button setTitle:@"主页" forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(returnToNext:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, CHEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.01];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = YES;
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

//- (void)returnToNext:(UIButton *)button{
//    CircleOfPetViewController *circle = [[CircleOfPetViewController alloc]init];
//    [self.navigationController pushViewController:circle animated:YES];
//    [circle release];
//}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (CHEIGHT == 480) {
        return CHEIGHT * 1.15;
    }else{
        return CHEIGHT;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailOfPhotographTableViewCell *detailOfPhotographCell = [DetailOfPhotographTableViewCell detailOfPhotopraphTableViewCellWithTableView:tableView];
    self.tableView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.01];

    if (self.detailOfPhotograph) {
    }
    detailOfPhotographCell.detailOfPhotograph = self.detailOfPhotograph;
    detailOfPhotographCell.delegate = self;
    
    return detailOfPhotographCell;
}

- (void)pushToPicNumPage:(NSString *)string{
    DetailOfNumViewController *detailOfNumViewController = [[DetailOfNumViewController alloc]init];
    
    detailOfNumViewController.pid = self.detailOfPhotograph.pets.pid;
    [self.navigationController pushViewController:detailOfNumViewController animated:YES];
    [detailOfNumViewController release];
}

- (void)pushToNextPage:(NSString *)string{
    StoryViewController *storyViewController = [[StoryViewController alloc]init];
    storyViewController.pid = self.detailOfPhotograph.pets.pid;
    [self.navigationController pushViewController:storyViewController animated:YES];
    [storyViewController release];
}

- (void)pushToFnumPage:(NSString *)string{
    FnumViewController *fnumViewController = [[FnumViewController alloc]init];
    fnumViewController.pid = self.detailOfPhotograph.pets.pid;
    [self.navigationController pushViewController:fnumViewController animated:YES];
    [fnumViewController release];
}

- (void)setPetname:(NSString *)petname{
    if (_petname != petname) {
        [_petname release];
        _petname = [petname retain];
    }
    self.navigationItem.title = petname;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//页面边缘手势导航栏变不透明,tableView布局发生变化
- (void)viewDidAppear:(BOOL)animated{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsCompact];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
